//
//  DetailViewController.m
//  Moosnick Museum Application
//
//  Created by Justin Blackburn, Mike McNary, Dan Bastin, Will Martin
//	Transylvania University 2010
//

#import "DetailViewController.h"
#import "MasterViewController.h"
#import <CoreData/CoreData.h>


@implementation DetailViewController

@synthesize navigationBar, popoverController, detailItem;
@synthesize itemImageView;
@synthesize title1;
@synthesize description;
@synthesize allData;
@synthesize url;
@synthesize webpageContent;
@synthesize error;

#pragma mark -
#pragma mark Managing the popover controller


-(id)init {
	[super initWithStyle:UITableViewStyleGrouped];
		NSLog(@"---> init");
	return self;
}

-(id)initWithStyle:(UITableViewStyle)style {
		NSLog(@"---> initWithStyle");
	return [self init];
}


/*
 When setting the detail item, update the view and dismiss the popover controller if it's showing.
 */
- (void)setDetailItem:(id)newDetailItem {
    if (detailItem != newDetailItem) {
        [detailItem release];
        detailItem = [newDetailItem retain];
        
        // Update the view.
        navigationBar.topItem.title = detailItem;
		NSString * imageName = [NSString stringWithFormat:@"%@.png",detailItem];
		[self.itemImageView setImage:[UIImage imageNamed:imageName]];
		
		// parse selected category page for items
		/*		
		if([detailItem isEqualToString:@"Specimens"])
			url = [NSURL URLWithString:@"http://www.cs.transy.edu/mmcnary/ana_spec_list.cgi"];
		
		if([detailItem isEqualToString:@"Instruments"])
			url = [NSURL URLWithString:@"http://www.cs.transy.edu/mmcnary/inst_list.cgi"];
		
		if([detailItem isEqualToString:@"Birds"])
			url = [NSURL URLWithString:@"http://www.cs.transy.edu/mmcnary/bird_list.cgi"];
		
		if([detailItem isEqualToString:@"Mammals"])
			url = [NSURL URLWithString:@"http://www.cs.transy.edu/mmcnary/mam_list.cgi"];
		
		if([detailItem isEqualToString:@"Minerals"])
			url = [NSURL URLWithString:@"http://www.cs.transy.edu/mmcnary/min_list.cgi"];
		
		if([detailItem isEqualToString:@"Other"])
			url = [NSURL URLWithString:@"http://www.cs.transy.edu/mmcnary/other_list.cgi"];
		*/
				
		url = [NSURL URLWithString:@"http://www.cs.transy.edu/dbastin/iPadlist.cgi"];
		webpageContent = [NSString stringWithContentsOfURL:url
												  encoding:NSASCIIStringEncoding
													 error:&error];
		
		allData = [webpageContent componentsSeparatedByString:@"|"];
		
		[allData removeObjectAtIndex:0];	
		[allData removeLastObject];
				
		self.title1.text = [allData objectAtIndex:2];
		self.description.text = [allData objectAtIndex:3];
    }

    if (popoverController != nil) {
        [popoverController dismissPopoverAnimated:YES];
    }        
}


#pragma mark -
#pragma mark Split view support


- (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc {
    
    barButtonItem.title = @"Categories";
    [navigationBar.topItem setLeftBarButtonItem:barButtonItem animated:YES];
    self.popoverController = pc;
}


// Called when the view is shown again in the split view, invalidating the button and popover controller.
- (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
    
    [navigationBar.topItem setLeftBarButtonItem:nil animated:YES];
    self.popoverController = nil;
}


#pragma mark -
#pragma mark Rotation support


// Ensure that the view controller supports rotation and that the split view can therefore show in both portrait and landscape.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}


#pragma mark -
#pragma mark View lifecycle


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}


- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.popoverController = nil;
}


#pragma mark -
#pragma mark Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSLog(@"all data count = %@", allData);
	return [allData count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"LocationCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...

    return cell;
}


#pragma mark -
#pragma mark Memory management


- (void)dealloc {
    [popoverController release];
    [navigationBar release];
    
    [detailItem release];
    [super dealloc];
}

@end
